var bingAPI = require('./BingAPI.01');
var bingProperties = require('./bingProperties');
var myDate = require('./myDate').myDate;
var ET = bingProperties.ET;
var t = require('./tools.js');
var ReportRequest = require('./report.01').ReportRequest;

var report = new ReportRequest();
var getDownload = new ReportRequest();

var SOAP = bingProperties.SOAP;
var STATIC = bingProperties.STATIC;
var REPORTTYPE = bingProperties.REPORTTYPE;
var wsdl = 'https://adcenterapi.microsoft.com/Api/Advertiser/v8/CampaignManagement/BulkService.svc?wsdl';
var customerAccountId = STATIC.customerAccountId;
var ACTION = bingProperties.ACTION;
var developerToken = STATIC.developerToken;
var password = STATIC.password;
var userName = STATIC.userName;
var downLoadPath = '/home/sonic/alibaba-work/nodejs/report/Bing/download/'

report.init('DownloadCampaignsByAccountIdsRequest');
report.root.find('.//CustomerAccountId').text = customerAccountId;
report.root.find('.//DeveloperToken').text = developerToken;
report.root.find('.//Password').text = password;
report.root.find('.//UserName').text = userName;
var account = ET.Element('{' + SOAP.ns_arrays + '}long');
account.text = customerAccountId;
report.root.find('.//AccountIds').append(account);
getDownload.init('GetDownloadStatusRequest');
getDownload.root.find('.//CustomerAccountId').text = customerAccountId;
getDownload.root.find('.//DeveloperToken').text = developerToken;
getDownload.root.find('.//Password').text = password;
getDownload.root.find('.//UserName').text = userName;


function main() {
    report.send(wsdl);
    report.on('end', function(chunk) {
        var data = chunk.toString();
        var xml = ET.XML(data);
        var downloadRequestId = xml.find('.//DownloadRequestId').text
        getDownload.root.find('.//DownloadRequestId').text = downloadRequestId;
        t.logTime('downloadRequestId = ' + downloadRequestId);
        getDownload.send(wsdl);
        getDownload.on('end', function(chunk) {
            var data = chunk;
            var xml = ET.XML(data);
            var status = xml.find('.//Status').text;
            switch(status) {
            case 'Failed':
                t.logTime('Failed');
                break;
            case 'InProgress':
                t.logTime('InProgress');
                setTimeout(function() {
                    getDownload.send(wsdl);
                }, 10000);
                break;
            case 'PartialSuccess':
                t.logTime('PartialSuccess');
                break;
            case 'Success':
                var dUrl = xml.find('.//DownloadUrl').text;
                t.logTime('Success');
                getDownload.downLoadFiled(dUrl, downLoadPath);
                break;
            }
        })
    });
    report.on('serverErrorEnd', function(chunk) {
        console.log(report.toXMLString())
        console.log(chunk);
    })
}

main();
// download(1737325);

function download(requetsId) {
    var downloadRequestId = requetsId; // 1736712
    getDownload.root.find('.//DownloadRequestId').text = downloadRequestId;
    // console.log(getDownload.toXMLString() );
    // return;
    getDownload.send(wsdl);
    getDownload.on('data', function(chunk) {
        var data = chunk.toString();
        var xml = ET.XML(data);
        var status = xml.find('.//Status').text;
        switch(status) {
        case 'Failed':
            t.logTime('Failed');
            break;
        case 'InProgress':
            t.logTime('InProgress');
            setTimeout(function() {
                getDownload.send(wsdl);
            }, 10000);
            break;
        case 'PartialSuccess':
            t.logTime('PartialSuccess');
            break;
        case 'Success':
            var dUrl = xml.find('.//DownloadUrl').text;
            t.logTime('Success');
            getDownload.downLoadFiled(dUrl, downLoadPath);
            break;
        }
    })
}